/**
 * Created by Administrator on 2017-11-04.
 */
Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {

    /**
     * 设置背景网格
     * @param config 配置
     */
    Paper.prototype.setGuide = function(config){
        //奇数行
        var oddLine = {
            'stroke': config.odd_line_color,
            'strokeWidth': config.odd_line_width,
            'stroke-opacity': config.odd_line_opacity / 10,
            'stroke-dasharray': config.odd_line_dasharray
        };
        //偶数行
        var evenLine = {
            'stroke': config.even_line_color,
            'strokeWidth': config.even_line_width,
            'stroke-opacity': config.even_line_opacity / 10,
            'stroke-dasharray': config.even_line_dasharray
        };

        if(config.switch == "ON"){
            for(var x=1; x<=config.width/config.transverse_space; x++){
                if(x%2 == 0){
                    this.line(x * config.transverse_space, 0, x * config.transverse_space, 1600).attr(oddLine);
                }else{
                    this.line(x * config.transverse_space, 0, x * config.transverse_space, 1600).attr(evenLine);
                }
            }

            for(var y=1; y<=config.height/config.portrait_space; y++){
                if(y%2 == 0){
                    this.line(0, y * config.portrait_space, 2400, y * config.portrait_space).attr(oddLine);
                }else{
                    this.line(0, y * config.portrait_space, 2400, y * config.portrait_space).attr(evenLine);
                }
            }
        }
    };

    /**
     * 医联体层级
     */
    Paper.prototype.mtc = function(id, name, count, config){
        var mtc = [];
        var marginX = config.marginX;   //边距X
        var marginY = config.marginY;   //边距Y
        var levelWidth = config.width - marginX * 2;                    //层级宽度
        var levelHeight = (config.height - marginY * 2) / count ;       //层级高度
        var levelTitleWidth = 60;                                                       //层级标题区宽度
        var levelContentWidth = 160;                                                    //层级内容区宽度
        var levelExhibitionWidth = levelWidth - levelTitleWidth - levelContentWidth;    //层级展示区宽度
        var levelNoMapping = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];    //层级映射

        for(var i=0; i<count; i++){
            var index = i + 1;
            var _level_title_x = marginX;
            var _level_title_y = marginY + (levelHeight * i);
            var _level_content_x = marginX + levelTitleWidth;
            var _level_content_y = marginY + (levelHeight * i);
            var _level_exhibition_x = marginX + levelTitleWidth + levelContentWidth;
            var _level_exhibition_y = marginY + (levelHeight * i);

            //标题区边框
            var titleFrame = this.rect(_level_title_x, _level_title_y, levelTitleWidth, levelHeight).attr({
                "id": "M-L-" + index + "-T-F",
                "stroke-width": 1,
                "fill": "#3498db",
                "stroke": "#34495e"
            });
            //标题区文本
            var titleText = this.text(titleFrame.getBBox().cx, titleFrame.getBBox().cy, [levelNoMapping[i], " ", "级"]).attr({
                "id": "M-L-" + index + "-T-T",
                "font-size": 12,
                "text-anchor": "middle",
                "fill": "#ffffff",
                "stroke": "#ffffff",
                "visibility": "visible"
            });
            //合并标题区元素
            var titleGroup = this.g(titleFrame, titleText);

            //内容区边框
            var middleFrame = this.rect(_level_content_x, _level_content_y, levelContentWidth, levelHeight).attr({
                "id": "M-L-" + index + "-M-F",
                "stroke-width": 1,
                "fill": "#34495e",
                "stroke": "#34495e",
                "fill-opacity": 0.2
            });
            //合并内容区元素
            var middleGroup = this.g(middleFrame);

            //展示区边框
            var exhibitionFrame = this.rect(_level_exhibition_x, _level_exhibition_y, levelExhibitionWidth, levelHeight).attr({
                "id": "M-L-" + index + "-E-F",
                "stroke-width": 1,
                "fill": "none",
                "stroke": "#34495e"
            });
            //合并展示区元素
            var exhibitionGroup = this.g(exhibitionFrame);

            //合并医联体层级元素
            var group = this.g();
            group.add(titleGroup, middleGroup, exhibitionGroup);
            group.attr({
                id: "M-L-" + index,
                name: name,
                type: "M-L"
            });
            mtc.push(group);
        }
        return mtc;
    };

    /**
     * 会员医院
     * @param id 主键
     * @param name 名称
     * @param mmhId 医联体会员医院ID
     * @param mmhLevel 层级
     */
    Paper.prototype.hospital = function(id, name, mmhId,  mmhLevel, config){
        var marginX = config.marginX;   //边距X
        var marginY = config.marginY;   //边距Y
        var x = config.x;
        var y = config.y;
        //边框
        var frame = this.rect(0, 0, 120, 60, 5).attr({
            id: "H-"+id+"-F",
            "stroke-width": 2,
            "fill": "#16a085",
            "stroke": "#4E5465"
        });
        var frameGroup = this.g(frame);
        //图标
        var icon = this.image("../../../public/images/h8.png", 0, 0, 20, 20).attr({
            id: "H-"+id+"-I"
        });
        var iconGroup = this.g(icon);
        iconGroup.transform(new Snap.Matrix().translate(50, 12));
        //文本
        var text = this.text(frameGroup.getBBox().cx, frameGroup.getBBox().cy + 17, name).attr({
            "id": "H-"+id+"-T",
            "font-size": 10,
            "text-anchor": "middle",
            "fill": "#ffffff",
            "visibility": "visible"
        });
        var textGroup = this.g(text);

        var group = this.g();
        group.add(frameGroup, iconGroup, textGroup);
        group.attr({
            id: "H-"+id,
            type: "H",
            mmhId: mmhId,
            mmhLevel: mmhLevel,
            cursor: "move"
        });
        x = x || (marginX + 60 + (160 - 120) / 2);
        y = y || (marginY + (180 * (mmhLevel - 1)) + (180 - 60) / 2);
        group.transform(new Snap.Matrix().translate(x, y));

        return group;
    };

    /**
     * 连线
     * @param type 类型(1:直线；2：曲线)
     * @param obj1 元素一
     * @param obj2 元素二
     * @param line 线条颜色
     * @param width 宽度
     * @returns {*}
     */
    Paper.prototype.connection = function(type, obj1, obj2, line, width){
        if(type == 1){
            return this.straight(obj1, obj2, line, width);
        }else{
            return this.curve(obj1, obj2, line, width);
        }
    };

    /**
     * 设置直线风格的连接线
     * @param obj1 元素一
     * @param obj2 元素二
     * @param line 线条颜色
     * @param width 宽度
     * @returns {*} 贝塞尔风格的连接线
     */
    Paper.prototype.straight = function(obj1, obj2, line, width){
        var bb1 = obj1.getBBox();
        var bb2 = obj2.getBBox();

        var x1 = bb1.x + bb1.width / 2;
        var y1 = bb1.y + bb1.height;
        var x2 = bb2.x + bb2.width / 2;
        var y2 = bb2.y;
        var path = "M"+x1+" "+y1+"L"+x2+" "+y2;
        var color = typeof line == "string" ? line : "#2c3e50";

        var path = this.path(path);
        path.attr({
            id: "C-"+obj1.attr("id")+"-"+obj2.attr("id"),
            type: "C",
            stroke: color,
            fill: "none",
            "stroke-width": width || 2,
            form: obj1.attr("id"),
            to: obj2.attr("id")
        });
        return path;
    };

    /**
     * 设置贝塞尔曲线风格的连接线
     * @param obj1 元素一
     * @param obj2 元素二
     * @param line 线条颜色
     * @param width 宽度
     * @returns {*} 贝塞尔风格的连接线
     */
    Paper.prototype.curve = function(obj1, obj2, line, width) {
        var bb1 = obj1.getBBox();
        var bb2 = obj2.getBBox();
        var p = [
            {x: bb1.x + bb1.width / 2, y: bb1.y - 1},
            {x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + 1},
            {x: bb1.x - 1, y: bb1.y + bb1.height / 2},
            {x: bb1.x + bb1.width + 1, y: bb1.y + bb1.height / 2},
            {x: bb2.x + bb2.width / 2, y: bb2.y - 1},
            {x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + 1},
            {x: bb2.x - 1, y: bb2.y + bb2.height / 2},
            {x: bb2.x + bb2.width + 1, y: bb2.y + bb2.height / 2}
        ];
        var d = {};
        var dis = [];

        for (var i = 0; i < 4; i++) {
            for (var j = 4; j < 8; j++) {
                var dx = Math.abs(p[i].x - p[j].x);
                var dy = Math.abs(p[i].y - p[j].y);
                if ((i == j - 4) || (((i != 3 && j != 6) || p[i].x < p[j].x) && ((i != 2 && j != 7) || p[i].x > p[j].x) && ((i != 0 && j != 5) || p[i].y > p[j].y) && ((i != 1 && j != 4) || p[i].y < p[j].y))) {
                    dis.push(dx + dy);
                    d[dis[dis.length - 1]] = [i, j];
                }
            }
        }
        if (dis.length == 0) {
            var res = [0, 4];
        } else {
            res = d[Math.min.apply(Math, dis)];
        }
        var x1 = p[res[0]].x;
        var y1 = p[res[0]].y;
        var x4 = p[res[1]].x;
        var y4 = p[res[1]].y;
        dx = Math.max(Math.abs(x1 - x4) / 2, 10);
        dy = Math.max(Math.abs(y1 - y4) / 2, 10);
        var x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3);
        var y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3);
        var x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3);
        var y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3);

        var path = "M"+x1+" "+y1+"C"+x2+" "+y2+","+x3+" "+y3+","+x4+" "+y4;
        var color = typeof line == "string" ? line : "#2c3e50";

        var path = this.path(path);
        path.attr({
            id: "C-"+obj1.attr("id")+"-"+obj2.attr("id"),
            type: "C",
            stroke: color,
            fill: "none",
            "stroke-width": width || 2,
            form: obj1.attr("id"),
            to: obj2.attr("id")
        });
        return path;
    };
});